<script type="text/javascript">

	        $(document).on("pagecreate","#pagenewfeed",function(){
 			 $("#mainwrapper").on("swiperight",function(){
    			 $("#sidePanel").panel( "open" );
  				});
	        });
		$(document).on("pagecreate","#pagewall",function(){
 			$("#a").on("swiperight",function(){
    			$("#myPanel").panel( "open" );
  			});
		});
		$(document).on("pagecreate","#pagecomment",function(){
 			 $("#a").on("swiperight",function(){
    			 $("#myPanel").panel( "open" );
  				});
		});
	
</script>

<script type="text/javascript">
	var count = 5;
	$(function() {
		var $win = $(window);
		$win.scroll(function() {
			if ($win.height() + $win.scrollTop() == $(document).height()){
				count+=5;
				for(i=0;i<count;i++){	 
				 $(".item"+i).show();}
			}
		});
	});
</script>

<script type="text/javascript">
	function clear_text(){ 
			$("#textarea-1").val('');
		}
		function go_comment(page_num){
			num = page_num;
			$.mobile.changePage('#pagecomment');
		}
		function load_content(post_id){
     		$.ajax({
	            type: "POST",
	            url: "<?php echo site_url('home/ajax_loadcomment');?>",
	            data: {"post_id" : post_id},
	            success: function(html){
	                $( "#show_content" ).html(html);
	            }
	        });
        	$.mobile.changePage('#pagecomment');
      	}
		$(document).on("pagebeforeshow","#pagewall",function(){
				for(i=0;i<count;i++){	 
				 $(".item"+i).show();
				}
		});
		$(document).on("pagebeforeshow","#pagenewfeed",function(){
				for(i=0;i<count;i++){	 
				 $(".item"+i).show();
				}
		});
		function check_like(str){
			  var temp = "like_btn"+str
			  if($("#"+temp).html()=='Like'){
			  	$("#"+temp).html('Unlike');
			  	if($("#status"+str).html()=='Be the first to like this.'){
			  		$("#status"+str).html('You Like');
			  	}else{
			  		$('#status'+str).prepend('You and ');
			  	}
			  }
			 else{
			 	$("#"+temp).html('Like');
			 	if($("#status"+str).html()=='You Like'){
			  		$("#status"+str).html('Be the first to like this.');
			  	}else{
			  		var Mystring =  $("#status"+str).html();
					var toRemove = 'You and ';
					$("#status"+str).html(Mystring.replace(toRemove,''));
			  	}

			 }
		}
</script>

<script type="text/javascript">
		function postButton_click(num){
			if ( $("#text_box").val() != "" ){
				var newPostID;
				var profilePic = "<? echo $fb['0']['fb_pic']; ?>";
				var profileName = "<? echo $fb['0']['fb_username']; ?>"; 
				var profileID = <? echo $fb['0']['fb_id']; ?>;
				var postPic = null;
				var time = new Date();
				var d = time.getDate();
				var m = time.getMonth() + 1;
				var y = time.getFullYear();
				var hours = time.getHours();
				var minute = time.getMinutes();
				var second = time.getSeconds();
				var timestamp = y + '-' + m + '-'+ '0' + d + ' ' + hours + ':' + minute + ':' + second;
				if ( num == 0 ){
					textStr = $("#text_box0").val();
				}
				else if ( num == 1 ){
					textStr = $("#text_box1").val();
				}
				$.ajax({
					type: "POST",
					url: "<? echo base_url("/home/ajax_post"); ?>",
					data: { 
						fb_id_poster: profileID,
						fb_id_wall: profileID,
						post_pic: postPic,
						post_content: textStr		
					} 
				})
				.done(function( msg ) {
					newPostID = msg;
					$.ajax({
						type: "POST",
						url: "<? echo base_url("/home/ajax_render_newfeed"); ?>",
						data: { 
							post_id: newPostID,
							profile_pic: profilePic,
							profile_name: profileName,
							fb_id_poster: profileID,
							fb_id_wall: profileID,
							post_pic: postPic,
							post_content: textStr,		
							timestamp: timestamp
						} 
					})
					.done(function( msg ) {
						if( num == 0){
							$("#feed_grid0").prepend( msg );
							$("#feed_grid1").prepend( msg );
							$("#text_box0").val("");
						}
						else if( num == 1){
							$("#feed_grid0").prepend( msg );
							$("#feed_grid1").prepend( msg );
							$("#text_box1").val("");
						}
					})
					.fail(function(){
						alert("Failed");
					});
				})
				.fail(function(){
				});
			}
		}
</script>

<script>
	function checkClick(num, bool, likeNo){
		if ( $('.like_button_'+num).html() == 'Like' ){
			click_like(num, bool, likeNo);
		}
		else if ( $('.like_button_'+num).html() == 'Unlike' ){
			click_unlike(num,bool, likeNo);
		}
		else{
			alert("error");
		}
	}
	function click_like(num, bool, likeNo){
		if ( $('.like_button_'+num).html() == 'Like' ){
			$('.like_button_'+num).html('Unlike');
		}
		else if ( $('.like_button_'+num).html() == 'Unlike' ){
			$('.like_button_'+num).html('Like');
		}
		if ( $('.total_like_'+num).html() == likeNo + ' people like this.' ){
			$('.total_like_'+num).html('You and ' + likeNo.toString() + ' people like this.');
		}
		else if ( $('.total_like_'+num).html() == 'Be the first to like this.'){
			$('.total_like_'+num).html('You like this');	
		} 
		else if ( $('.total_like_'+num).html() == likeNo-1 + ' people like this.' ){
			var str = 'You and ' + (likeNo-1).toString() + ' people like this.';
			$('.total_like_'+num).html(str);	
		}
		var post_id = num;
		var fb_id = <? echo $fb['0']['fb_id']; ?>;
		var ajax_insert_url = "<? echo base_url("/home/ajax_insert_likep/"); ?>";
		$.ajax({
			type: "POST",
			url: ajax_insert_url,
			data: {	
				post_id: post_id,		
				fb_id: fb_id
			}
		})
		.done(function( msg) {
			var ajax_update_url = "<? echo base_url("/home/ajax_update_likep/"); ?>";
			$.ajax({
				type: "POST",
				url: ajax_update_url,
				data: {	
					post_id: post_id,		
				}
			})
			.done(function( msg) {
			});
		});
	}	
	
	function click_unlike(num, bool, likeNo){
		if ( $('.like_button_'+num).html() == 'Like' ){
			$('.like_button_'+num).html('Unlike');
		}
		else if ( $('.like_button_'+num).html() == 'Unlike' ){
			$('.like_button_'+num).html('Like');
		}
		if ( $('.total_like_'+num).html() == 'You and ' + likeNo.toString() + ' people like this.'){
			$('.total_like_'+num).html(likeNo.toString() + ' people like this.');
		}
		else if ( bool == 1 && likeNo != 1){
			$('.total_like_'+num).html(likeNo-1 + " people like this.");
		}
		else if ( bool ==1 && likeNo == 1){
			$('.total_like_'+num).html("Be the first to like this.");
		}
		else if ( bool == 0 ){
			$('.total_like_'+num).html("Be the first to like this.");
		}
		var post_id = num;
		var fb_id = <? echo $fb['0']['fb_id']; ?>;
		var ajax_delete_url = "<? echo base_url("/home/ajax_delete_likep/"); ?>";
		$.ajax({
			type: "POST",
			url: ajax_delete_url,
			data: {	
				post_id: post_id,		
				fb_id: fb_id
			}
		})
		.done(function( msg) {
			var ajax_update_url = "<? echo base_url("/home/ajax_update_likep/"); ?>";
			$.ajax({
				type: "POST",
				url: ajax_update_url,
				data: {	
					post_id: post_id,		
				}
			})
			.done(function( msg) {
			});

		});
	}
</script>

